<?php
/**
 * Exceptions Functions
 * @filesource
 */
/**
 * Namespace
 */
namespace framework\library;
/**
 * Dependencies
 */
use \framework\library\Log;
/**
 * Exceptions Functions
 *
 * @author Randy Casburn <randy@plurious.com>
 * @copyright Copyright Plurious Software, all rights reserved
 * @license MIT
 * @license http://www.opensource.org/licenses/mit-license.html
 * @link http://plurious.com/framework/documentation/classes/Front.html
 * @version 2.1
 * 
 * @uses \framework\library\Log
 */

/** Define degugger detail */
define('DEBUG_DETAIL_OFF', true);
/**
 * Global Error handler for Plurious framework
 * 
 * @param int $errno The PHP error code number
 * @param string $errstr The Error message
 * @param string $errfile The file the error was triggered from
 * @param int $errline The Line number the error was triggered from
 * @return bool Whether details are turned on or off 
 */
function error_handler($errno, $errstr, $errfile, $errline) {
    $log = new Log();

    switch ($errno) {
        case E_NOTICE:
        case E_USER_NOTICE:
            $error = 'Notice';
            break;
        case E_WARNING:
        case E_USER_WARNING:
            $error = 'Warning';
            break;
        case E_ERROR:
        case E_USER_ERROR:
            $error = 'Fatal Error';
            break;
        default:
            $error = 'Exception Thrown';
            break;
    }

    $msg = "===============================================================\n";
    $msg .= "<b>FRAMEWORK: $error \n in: <b> $errfile </b>  on line: <b> $errline </b>\n<b>--></b>: " . $errstr . "\n";
    $msg .= "===============================================================\n";

    if (DEBUG) {
        echo nl2br($msg);
        $log->write(strip_tags($msg));
    }
    return DEBUG_DETAIL_OFF;
}
/**
 * Passes all exceptions to the error handler
 * 
 * @param object $e 
 */
function exception_handler($e) {
    error_handler($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
}

// Set the Global Error Handler
set_error_handler('\\framework\\library\\error_handler');
//set_exception_handler('\\framework\\library\\exception_handler');

// Set the global Error Reporting state
if (DEBUG) {
    error_reporting(E_ALL);
} else {
    error_reporting(0);
}
?>